package ro.cityprojects.adminloc.service;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import ro.cityprojects.adminloc.dao.BasicRepository;
import ro.cityprojects.adminloc.entities.Apartment;
import ro.cityprojects.adminloc.entities.Entrance;
import ro.cityprojects.adminloc.exceptions.EntityNotFoundException;
import ro.cityprojects.adminloc.exceptions.ValidationException;
import ro.cityprojects.adminloc.metadata.EntityMetadata;

import java.util.Set;

/**
 * ApartmentsServiceImpl
 *
 * @author bogdan.solga
 *
 * Date: 17/10/13, time: 18:00
 */
@Service
public class ApartmentsServiceImpl extends BaseService implements ApartmentsService {

    @Autowired
    private BasicRepository<Integer, Apartment> repository;

    @Autowired
    private BasicRepository<Integer, Entrance> entranceRepository;

    @Override
    public String[] getDefaultFields() {
        return new String[]{"id", "entranceId", "identifier", "alias", "numberOfPersons", "surface"};
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public void save(Apartment apartment) throws ValidationException {
        //TODO: use a drop-down
        Entrance entrance = entranceRepository.get(Entrance.class, 1);
        apartment.setEntranceByEntranceId(entrance);

        repository.save(apartment);
    }

    @Override
    @Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
    public String get(Integer primaryKey, String includedFields) throws EntityNotFoundException {
        Apartment apartment = repository.get(Apartment.class, primaryKey);

        String[] outputFields = getOutputFields(includedFields, getDefaultFields());

        return serialize(apartment, outputFields);
    }

    @Override
    @Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
    public String getAll(String filterName, String filterValue, String includedFields) {
        Set<Apartment> all = repository.getAll(Apartment.class, filterName, filterValue);
        Assert.notEmpty(all, "There are no Apartments with the specified details");

        String[] outputFields = getOutputFields(includedFields, getDefaultFields());

        return serialize(all, outputFields);
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public void update(Apartment apartment, Integer primaryKey) throws ValidationException {
        Apartment updated = repository.get(Apartment.class, primaryKey);

        BeanUtils.copyProperties(apartment, updated);

        repository.update(updated);
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public void delete(Integer primaryKey) throws EntityNotFoundException {
        repository.delete(Apartment.class, primaryKey);
    }

    @Override
    @Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
    public void checkExisting(Apartment apartment) throws ValidationException {
        String alias = apartment.getAlias();
        Apartment existing = repository.getByProperty(Apartment.class, "alias", alias);
        if (existing != null) {
            throw new ValidationException("Exista deja un apartament cu alias-ul '" + alias + "'");
        }
    }

    @Override
    public String getMetadata() {
        return getMetadata(EntityMetadata.Apartments);
    }
}
